package br.com.center.loja.mb.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import br.com.center.loja.data.RepositoryFactoryJPA;
import br.com.center.loja.model.Client;

@FacesConverter(forClass=Client.class, value="clientConverter")
public class ClientConverter implements Converter 
{
	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) 
	{
		try 
		{
			return RepositoryFactoryJPA.create(Client.class).select(Long.valueOf(value));			
		} 
		catch (Exception e) 
		{
			return null;
		}
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value) 
	{
		try 
		{
			return ((Client)value).getCode().toString();
		} 
		catch (Exception e) 
		{		
			return null;
		}
	}

}
